<template>
    <div class="qingwu">
        <div class="admin_table_page_title">业主管理</div>
        <div class="unline underm"></div>


        <div class="admin_table_list">
            <a-table :columns="columns" :data-source="data" :pagination="false" :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" row-key="id">
                <span slot="action" slot-scope="rows">
                    <a-button icon="edit" @click="$router.push('/Admin/property/form/'+rows.id)">编辑</a-button>
                </span>
                <span slot="status" slot-scope="rows">
                    <a-switch v-if="rows.status == 1" checked-children="正常" un-checked-children="禁用" @change="onChangeStatus(rows.id)" default-checked/>
                    <a-switch v-else checked-children="正常" @change="onChangeStatus(rows.id)" un-checked-children="禁用" />
                </span>
            </a-table>
            <div class="admin_pagination" v-if="total > 0">
                <a-pagination v-model="params.page" :page-size.sync="params.per_page" :total="total" @change="onChange" show-less-items />
            </div>
        </div>
    </div>
</template>

<script>

export default {
    components: {
    },
    props: {},
    data() {
        return {
            params:{
                page:1,
                per_page:30,
                check: 1
            },
            total:0, //总页数
            selectedRowKeys:[], // 被选择的行
            data:[],
            columns:[
                {title:'#',dataIndex:'id',fixed:'left',width:50},
                {title: '物业名称', dataIndex: 'name', width:300},
                {title: '登陆账号', dataIndex: 'account' },
                {title: '公司地址', dataIndex: 'address',ellipsis: true},
                {title: '负责人',dataIndex: 'principal'},
                {title: '负责人电话',dataIndex: 'principal_mobile'},
                {title: '状态', scopedSlots: { customRender: 'status' }},
                {title: '添加时间', dataIndex: 'create_time'},
                {title: '操作',fixed:'right',scopedSlots: { customRender: 'action' }},
            ],
            name:'', // 物业名称
            mobile:'', // 负责人电话
            status:'', // 状态
        };
    },
    watch: {},
    computed: {},
    methods: {
        // 选择框被点击
        onSelectChange(selectedRowKeys) {
            this.selectedRowKeys = selectedRowKeys;
        },
        // 选择分页
        onChange(e){
            this.params.page = e;
        },
        // 初始化事件
        onload(){
            this.$get(this.$api.adminProperty,this.params).then(res=>{
                this.total = res.data.total;
                this.data = res.data.data;
            });
        },
        handleChange(value){
            this.status = value;
        },

        // 搜索
        search(){
            this.params['name'] = this.name;
            this.params['mobile'] = this.mobile;
            this.params['status'] = this.status;
            this.$get(this.$api.adminProperty,this.params).then(res=>{
                this.total = res.data.total;
                this.data = res.data.data;
            });
        },
        // 重置
        reset(){
            location.reload();
        },

        // 修改状态
        onChangeStatus(id){
            this.$post(this.$api.editPropertyStatus,{id:id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.msg)
                }else{
                    return this.$message.error(res.msg)
                }
            })
        },

        // 删除
        del(){
            if(this.selectedRowKeys.length==0){
                return this.$message.error('未选择数据.');
            }
            this.$confirm({
                title: '你确定要删除选择的数据？',
                content: '确定删除后无法恢复.',
                okText: '是',
                okType: 'danger',
                cancelText: '取消',
                onOk:()=> {
                    let ids = this.selectedRowKeys.join(',');
                    this.$delete(this.$api.adminProperty+'/'+ids).then(res=>{
                        if(res.code == 200){
                            this.onload();
                            this.$message.success('删除成功');
                        }else{
                            this.$message.error(res.msg)
                        }
                    });

                },
            });
        },

    },
    created() {
        this.onload();
    },
    mounted() {

    },

};
</script>
<style lang="scss" scoped>

</style>
